#ifndef __MAIN_H__
#define __MAIN_H__

#include "sqlite3.h"

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <termios.h>
#include <math.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include "queue.h"

extern int fd;
extern int flag;

extern pthread_mutex_t mutex1;
extern pthread_mutex_t mutex2;
extern pthread_mutex_t mutex3;
extern pthread_mutex_t mutex4;
extern pthread_cond_t cond;

void serial_init(int fd);
void *zig_read(void *date);
void * pick(void *date);
int suit();
void * sqlite_back(void *date);

int sem_p(int semid, int num);
int sem_v(int semid, int num);
float to_float(uint8_t n[]);
void *shrmem_back(void *date);

void *msgque_get(void *date);
void *zig_write(void *date);
void *thld_info(void *date);
			
struct Env
{
	uint8_t head[3]; //标识位st:
	uint8_t type;//数据类型
	uint8_t snum; //仓库编号
	uint8_t temp[2]; //type温度
	uint8_t hum[2]; //湿度
	uint8_t x; //三轴信息
	uint8_t y; 
	uint8_t z; 
	uint32_t ill; //光照
	uint32_t bet; //temp电池电量
	uint32_t adc;  //电位器信息
};

struct Env recv;

#define ENV 0
#define Cargo 1

//消息队列结构体
typedef struct msg_queue{
	long type;  	      //消息类型
	long hal;   	      //要操作的硬件
	unsigned char text[32]; //对设备的操作
}msg_t;

msg_t *msg_buf;

//货物信息结构体
typedef struct goods_status{
	uint8_t  goods_type;
	uint32_t goods_count;
}goods_msg;

struct env_info{
	uint8_t storage_status;
	uint8_t led_status;
	uint8_t buzzer_status;
	uint8_t fan_status;
	uint8_t seg_status;
	char    x;
	char    y;
	char    z;
	char    samplingTime[20];
	float   temperature;
	float   temperatureMIN;
	float   temperatureMAX;
	float   humidity;
	float   humidityMIN;
	float   humidityMAX;
	float   illumination;
	float   illuminationMIN;
	float   illuminationMAX;
	float   battery;
	float   adc;
	float   adcMIN;
	goods_msg goods_info[10];
};

struct env_info_clien_addr{
	struct env_info storage_no[2];
};

struct shm_addr
{
	char cgi_status;
	char qt_status;
	struct env_info_clien_addr rt_status;
};

struct shm_addr *shm_buf;

/*
//阈值信息结构体
typedef struct threshold_info{
	int temperature_max; 	 //温度上限
	int temperature_min; 	 //温度下限
	int humidity_max;    	 //湿度上限
	int humidity_min;    	 //温度下限
	int light_max;          //最大光照强度
	int light_min;          //最小光照强度
}threshold_t;
*/

#endif
